import request from "@/utils/request.ts";
import type {Detail, DetailPage, Dictionary, SelectForm} from "@/api/detail/type.ts";
import type {SysResponse,} from "@/api/commonType.ts";
import type {AxiosResponse} from "axios";


enum API {
  //获取详情列表
  GET_DETAILS = '/api/detail/page',
  GET_DIC_INCOME_EXPENSE_TYPE = '/api/detail/dic/incomeExpenseType',
  GET_DIC_SOURCE_TYPE = '/api/detail/dic/sourceType',
  SAVE='/api/detail/save',
  DELETE='/api/detail/delete',
  EXPORT='/api/detail/export',
}


/**
 * 获取详情分页
 * @param data
 */
export function getDetailPage(data: SelectForm) {
  return request.post<null, SysResponse<DetailPage>>(API.GET_DETAILS, data);
}

/**
 * 获取收入支出类型字典
 */
export function getIncomeExpenseType() {
  return request.get<null, SysResponse<Dictionary[]>>(API.GET_DIC_INCOME_EXPENSE_TYPE);
}


/**
 * 获取来源类型字典
 */
export function getSourceType() {
  return request.get<null, SysResponse<Dictionary[]>>(API.GET_DIC_SOURCE_TYPE);
}

/**
 * 修改详情
 * @param data
 */
export function saveDetail(data: Detail) {
  return request.post<null, SysResponse<Detail>>(API.SAVE, data);
}

/**
 * 删除详情
 * @param id
 */
export function deleteDetail(id: number) {
  return request.delete<null, SysResponse<Detail>>(API.DELETE+`/${id}`);
}

/**
 * 导出详情
 * @param data
 */
export function exportDetail(data: SelectForm) {
  return request({
    url: API.EXPORT,
    method: 'GET',
    params: {
      ...data,
      pageNum: undefined,
      pageSize: undefined
    },
    responseType: 'blob',
    // 确保获取完整响应
    transformResponse: res => res
  })as Promise<AxiosResponse<Blob>>;
}
